package org.bendev.ildaplayer;

import java.util.Calendar;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class TestPlayerActivity extends Activity  {
	static final String TAG = "TestPlayerActivity";

	Player player;
	GestureHandler gh;

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );

		setContentView( R.layout.activity_player );
		final ImageView iv = (ImageView) findViewById( R.id.imageview );
		final VideoView vv = (VideoView) findViewById( R.id.videoview );
/*
		player = new AbstractPlayer(){
			String channel;
			boolean isPlaying = false, isLoadingFrame = false;
			Calendar pos, startTime, pendingSeek = null;
			Object lock = new Object();
			
			@Override
			public void play( String channel, Calendar start ) {
				this.channel = channel;
				seekTo( start );
				start();
			}

			@Override
			public void stop() {
				pause();
			}

			@Override
			public void pause() {
				pos = getPosition();
				isPlaying = false;
				onStateChange();
			}

			@Override
			public void start() {
				isPlaying = true;
				startTime = Calendar.getInstance();
				onStateChange();
			}

			@Override
			public State getState() {
				return isPlaying ? State.PLAYING : State.PAUSED;
			}

			@Override
			public Calendar getPosition() {
				return isPlaying 
					? DateUtils.add( pos, Calendar.getInstance().getTimeInMillis() - startTime.getTimeInMillis() )
					: pos;
			}

			@Override
			public void seekTo( Calendar pos ) {
				this.pos = pos;
				synchronized(lock){
					if( isLoadingFrame ){
						pendingSeek = pos;
						return;
					}
					isLoadingFrame = true;
				}
				//getFrame( pos );
			}
			
			void getFrame( Calendar pos ){
				String uri = SegmentPlayer.getFrameUri(channel,pos);
				Log.i(TAG, "getting frame "+uri);
				RemoteBitmap.get( Uri.parse( uri ), new RemoteBitmap.Callback() {
					@Override
					public void onBitmapError( Exception ex ) {
						onBitmapDone();
					}
					
					@Override
					public void onBitmap( Bitmap bm ) {
						iv.setImageBitmap( bm );
						iv.setVisibility( View.VISIBLE );
						onBitmapDone();
					}
					
					void onBitmapDone(){
						synchronized(lock){
							if( pendingSeek==null )
								isLoadingFrame = false;
							else{
								getFrame( pendingSeek );
								pendingSeek = null;
							}
						}
					}
				} );
			}
			
		};
		vv.setVideoSize( 512, 288 );
*/
		
		SegmentPlayer p = new SegmentPlayer( this );
		p.setImageView( iv );
		p.setVideoView( vv );
		player = p;
		
		StatusView sv = (StatusView) findViewById( R.id.statusview );
		gh = new GestureHandler( this, sv );

		sv.setPlayer( player );
		player.play( "France2", DateUtils.add( Calendar.getInstance(), Calendar.HOUR, -2 ) );
	}

	@Override
	protected void onPause() {
		super.onPause();
		player.stop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		player.stop();
	}

	@Override
	public boolean onCreateOptionsMenu( Menu menu ) {
		getMenuInflater().inflate( R.menu.main, menu );
		return true;
	}
	
	@Override
	public boolean onKeyDown( int keyCode, KeyEvent event ) {
		Log.i( TAG, "onkeydown" );
		boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK && keyCode != KeyEvent.KEYCODE_VOLUME_UP && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN
				&& keyCode != KeyEvent.KEYCODE_MENU && keyCode != KeyEvent.KEYCODE_CALL && keyCode != KeyEvent.KEYCODE_ENDCALL;
		if( isKeyCodeSupported ){

		}
		return super.onKeyDown( keyCode, event );
	}

	@Override
	public boolean onTouchEvent( MotionEvent event ) {
		gh.onTouchEvent( event );
		return true;
	}
}
